//
// File: server.cpp
// Description: 
//

#include <iostream>
#include <string>

using namespace std;

#include "protocol.h"
#include "socket.h"
#include "client.h"
#include "database.h"

//////////////////////
// ~ MAIN - birdo was here
//////////////////////
DWORD WINAPI clientThread(void *param);

int main(int argc, char **argv) {
	
	if (argc < 2) {
		cerr << "Usage: " << argv[0] << " port" << endl;
		exit(0);
	}
	
	Database & db = Database::get();
	if (db.createUser("leaf", "leaf")) cout << "Added user 'leaf'" << endl;

	
	int port = atoi(argv[1]);
	ServerSocket server(port);
	
	char buffer[512];
	Socket *ns;
	for (;;) {
		ns = server.accept();
		CreateThread(NULL, 0, clientThread, ns, 0, 0);
		
	}
	
	server.close();
	server.closeWinsock();
}

DWORD WINAPI clientThread(void *param) {
	Socket & s = *(Socket*)param;
	
	Client c(s);
	c.run();
	
	s.close();
	delete &s;
	
}
